package com.llkj.youdaocar;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.llkj.youdaocar.httpUtils.HttpMethod;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 引导页
 */
public class AdActivity extends BaseActivity {
    @BindView(R.id.tvIgnore)
    TextView tvIgnore;
    @BindView(R.id.logo)
    ImageView ivPic;
    private String linkUrl;
    ValueAnimator objectAnimator;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        systemBarTintManager.setStatusBarTintResource(R.color.transparent);
        ButterKnife.bind(this);
        tvIgnore.setVisibility(View.VISIBLE);

//        showWaitDialog();
        HttpMethod.ad(this,this,"4");
    }

    @OnClick({R.id.tvIgnore,R.id.logo})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.tvIgnore:
                doFinish(null);
                break;
            case R.id.logo:
                doFinish(linkUrl);
                break;
        }
    }
    private void  initAnim() {
        objectAnimator = ValueAnimator.ofInt(0,5000);
        objectAnimator.setDuration(5000);
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (int) valueAnimator.getAnimatedValue();
                int time= 5-value/1000;
                tvIgnore.setText("跳过"+time);
                if(value==5000){
                    doFinish(null);
                }
            }
        });
        objectAnimator.start();
    };

    private void doFinish(String url) {
        if(objectAnimator!=null && objectAnimator.isRunning()){
            objectAnimator.cancel();
        }
        try {
            Intent intent = new Intent(this,MainActivity.class);
            if(!TextUtils.isEmpty(url)){
                intent.putExtra("url",url);
            }
            startActivity(intent);
            finish();
        }catch (Exception e){}
    }

    @Override
    public void onhttpSuccess(String data, int method) {
        if(method== HttpMethod.HTTP_LOGIN_AD){
            try {
                data = data.replace("\n","");
                data =data.replace("\t","");
                JSONObject jsonObject = new JSONObject(data);
                if (jsonObject.has("data")) {
                    JSONArray jsonData = jsonObject.optJSONArray("data");
                    jsonObject = jsonData.getJSONObject(0);
                    if(jsonObject.has("link")){
                        linkUrl = jsonObject.optString("link");
                    }
                    if(jsonObject.has("img")){
                        String imgUrl = jsonObject.optString("img");
                        Glide.with(this)
                                .load(imgUrl)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .crossFade()
                                .into(ivPic);
                        initAnim();
                    }

                }
            } catch (JSONException e) {
            }
        }
    }
}
